package cn.edu.abtu.service.impl;

import cn.edu.abtu.entity.SysDepartment;
import cn.edu.abtu.entity.SysUser;
import cn.edu.abtu.mapper.SysUserMapper;
import cn.edu.abtu.service.SysDepartmentService;
import cn.edu.abtu.mapper.SysDepartmentMapper;
import cn.edu.abtu.utils.AjaxResult;
import cn.edu.abtu.utils.DepartmentTree;
import cn.edu.abtu.vo.query.DepartmentQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * (SysDepartment)表服务实现类
 *
 * @author makejava
 * @since 2022-11-16 11:48:24
 */
@Slf4j
@Service
public class SysDepartmentServiceImpl extends ServiceImpl<SysDepartmentMapper, SysDepartment> implements SysDepartmentService {

    @Resource
    private SysDepartmentMapper sysDepartmentMapper;

    @Resource
    private SysUserMapper sysUserMapper;

    // 查询部门菜单列表
    @Override
    public List<SysDepartment> getDepartmentList(DepartmentQueryVo departmentQueryVo) {
        QueryWrapper<SysDepartment> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(Objects.nonNull(departmentQueryVo.getDepartmentName()), "department_name", departmentQueryVo.getDepartmentName());
        queryWrapper.orderByAsc("order_num");
        List<SysDepartment> departmentList = sysDepartmentMapper.selectList(queryWrapper);
        List<SysDepartment> departmentTree = new ArrayList<>();
        if (!departmentList.isEmpty()){
            // 找到集合中最小的parentId的值
            long parentId = departmentList.stream().mapToLong(item -> item.getParentId()).min().getAsLong();
            departmentTree = DepartmentTree.makeDepartmentTree(departmentList, parentId);
        }
        return departmentTree;
    }

    // 查询上级部门列表
    @Override
    public List<SysDepartment> getParentDepartment() {
        QueryWrapper<SysDepartment> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("order_num");
        List<SysDepartment> departmentList = sysDepartmentMapper.selectList(queryWrapper);
        SysDepartment department = new SysDepartment();
        department.setId(0L);
        department.setDepartmentName("顶级部门");
        department.setParentId(-1L);
        departmentList.add(department);
        List<SysDepartment> departmentTree = DepartmentTree.makeDepartmentTree(departmentList, -1L);
        return departmentTree;
    }


    @Override
    public AjaxResult removeById(Long id) {
        // 判断该部门下是否存在子部门
        QueryWrapper<SysDepartment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", id);
        Long count = sysDepartmentMapper.selectCount(queryWrapper);
        if (count>0){
            return AjaxResult.error("该部门下存在子部门,不能删除!",null);
        }

        QueryWrapper<SysUser> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("department_id", id);
        Long userCount = sysUserMapper.selectCount(userQueryWrapper);
        if (userCount>0){
            return AjaxResult.error("该部门下存用户,不能删除!",null);
        }

        int result = sysDepartmentMapper.deleteById(id);
        if (result>0){
            return AjaxResult.success("部门删除成功!",null);
        }
        return AjaxResult.error("部门删除失败!",null);
    }
}
